/*
 * TableLogHandlerPanel.java
 *
 * Created on 2008. március 8., 20:19
 */

package orkomet;

import java.awt.Point;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  Adam
 */
public class JTableLogHandlerPanel extends javax.swing.JPanel {
    
    private int maxRows=100;
    /** Creates new form TableLogHandlerPanel */
    public JTableLogHandlerPanel() {
        initComponents();
    }
    
    public void registerLogger(String lgName)
    {
        Logger.getLogger(lgName).addHandler(new TableLogHandler());
    }
    
    private class TableLogHandler extends Handler
    {
      public void publish(LogRecord logRecord) {
          String[] newRecord=new String[2];
          newRecord[0]=DateFormat.getInstance().format(new Date(logRecord.getMillis()));
          newRecord[1]=logRecord.getMessage();
          DefaultTableModel dModel= (DefaultTableModel)(jtblLogtable.getModel());
          dModel.addRow(newRecord);
          if(jtblLogtable.getRowCount()>=maxRows)
            dModel.removeRow(0);           
      }

      public void flush() {
              
      }
      
      public void close() {
          
      }
    }
    
    
    private class LogCellRenderer extends DefaultTableCellRenderer{
        String message;

             
        @Override
        public void setValue(Object value) {
            message=value.toString();
            setText(message);
            setToolTipText(message);
        }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jtblLogtable = new javax.swing.JTable();

        jtblLogtable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Time", "Message"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtblLogtable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtblLogtableMouseClicked(evt);
            }
        });
        jScrollPane2.setViewportView(jtblLogtable);
        jtblLogtable.getColumnModel().getColumn(1).setCellRenderer(new LogCellRenderer());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 389, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addGap(12, 12, 12)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 223, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(2, 2, 2)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(2, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jtblLogtableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtblLogtableMouseClicked
        int col = jtblLogtable.columnAtPoint(new Point(evt.getX(), evt.getY()));
        if(!jtblLogtable.getColumnName(col).equals("Message")) return;
        int row = jtblLogtable.rowAtPoint(new Point(evt.getX(), evt.getY()));
        JOptionPane.showMessageDialog(jScrollPane2, jtblLogtable.getValueAt(row, col),getName(),JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_jtblLogtableMouseClicked
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jtblLogtable;
    // End of variables declaration//GEN-END:variables
    
}
